﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;

namespace MvcMall.AdminWeb.Manage.GoodsBrands
{
    public partial class Index : ManageBasePage
    {
        private IGoodsBrandTask _goodsBrandTask;
        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("GoodsBrandIndex");
            _goodsBrandTask = GetInstance<IGoodsBrandTask>();

            if (!IsPostBack)
            {
                BindRpt();
            }
        }

        private void BindRpt()
        {
            var list = _goodsBrandTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize);
            var dtoList = list.Select(c => new GoodsBrandDto
                                               {
                                                   AdminUserName =
                                                       c.AdminUser == null ? string.Empty : c.AdminUser.UserName,
                                                   BrandName = c.BrandName,
                                                   CatName = c.GoodsCat.CatName,
                                                   CreateTime = c.CreateTime,
                                                   GoodsBrandStatus = c.GoodsBrandStatus,
                                                   Id = c.Id,
                                                   ModifyTime = c.ModifyTime,
                                                   SortOrder = c.SortOrder,
                                                   UserName = c.Store == null ? string.Empty : c.Store.User.UserName
                                               }).ToList();
            rptBrands.DataSource = dtoList;
            rptBrands.DataBind();
            ANPager.RecordCount = list.TotalItemCount;
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }
    }
}